
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router"

//缓存
import store from './../store/index'

import Home from "../views/Home/Home.vue"

import NewsDetail from "../views/Collect/newsDetail.vue"

import Collect from "../views/Collect/Collect.vue"

import ProjectDetail from "../views/User/ProjectDetail.vue"

import User from '../views/User/User.vue'

import Login from '../views/Login/Login.vue'

import ErrorVue from '../views/ErrorVue.vue'

import SetPassword from '../views/User/SetPassword.vue'

import UserRegister from '../views/User/UserRegister.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: "/Home",
    name: "Home",
    component: Home,
    meta: {
      login: false,
      title: "新闻-首页"
    }
  },
  {
    path: "/",
    redirect: '/Home'
  },
  {
    path: "/Login",
    name: "Login",
    component: Login,
    meta: {
      login: false,
      title: "M·登录"
    }
  },
  {
    path: "/newsDetail",
    name: "newsDetail",
    component: NewsDetail,
    meta: {
      login: false,
      title: "新闻详情"
    }
  },
  {
    path: "/ProjectDetail",
    name: "ProjectDetail",
    component: ProjectDetail,
    meta: {
      login: false,
      title: "项目详情"
    }
  },
  {
    path: "/Collect",
    name: "Collect",
    component: Collect,
    meta: {
      login: true,
      title: "收藏列表"
    }
  },
  {
    path: "/User",
    name: "User",
    component: User,
    meta: {
      login: false,
      title: "个人中心"
    }
  },{
    path: "/SetPassword",
    name: "SetPassword",
    component: SetPassword,
    meta: {
      login: true,
      title: "修改密码"
    }
  },{
    path: "/UserRegister",
    name: "UserRegister",
    component: UserRegister,
    meta: {
      login: false,
      title: "用户注册"
    }
  },
  {
    path: "/:pathMatch(.*)*",
    name: '网页异常',
    component: ErrorVue,
    meta: {
      logo: false,
      title:'网页异常'
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from,next) => {
  let vuex_state = JSON.parse(JSON.stringify(store.state))
  if(to.meta.login) {
    if (vuex_state.userData.userCode == '') {
      next({ path: '/login' })
    } else {
      next()
    }
  } else {
    next()
  }
  (<any>document).title = to.meta.title
})
export default router